package top.lixunda.commerce.common.core.controller;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import top.lixunda.commerce.common.core.controller.wrapper.BaseDataQuerySqlWrapper;
import top.lixunda.commerce.common.core.controller.wrapper.BaseDataUpdateSqlWrapper;
import top.lixunda.commerce.common.core.controller.wrapper.EntityUpdateWrapper;
import top.lixunda.commerce.common.core.controller.wrapper.PageWrapper;
import top.lixunda.commerce.common.core.entity.MessageObject;
import top.lixunda.commerce.common.core.exception.InternalApiException;

import java.util.List;
import java.util.Map;

/**
 * @author LiXunda
 * @version time:2019/10/11 20:14
 */
@SuppressWarnings({"WeakerAccess", "unused"})
@Slf4j
public class BaseController<M extends ServiceImpl<N, T>, N extends BaseMapper<T>, T> implements IController<T> {

    @SuppressWarnings("SpringJavaAutowiredMembersInspection")
    @Autowired
    protected M m;

    public IService<T> getBaseService() {
        return m;
    }

    public BaseMapper<T> getBaseMapper() {
        return m.getBaseMapper();
    }

    @Override
    public MessageObject<Boolean> save(T entity) {
        Boolean success = this.getBaseService().save(entity);
        return MessageObject.success(success);
    }

    @Override
    public MessageObject<Boolean> saveBatch(List<T> entityList, int batchSize) {
        Boolean success = getBaseService().saveBatch(entityList, batchSize);
        return MessageObject.success(success);
    }

    @Override
    public MessageObject<Boolean> saveBatch(List<T> entityList) {
        Boolean success = this.getBaseService().saveBatch(entityList);
        return MessageObject.success(success);
    }

    @Override
    public MessageObject<Boolean> saveOrUpdateBatch(List<T> entityList, int batchSize) {
        Boolean success = getBaseService().saveOrUpdateBatch(entityList, batchSize);
        return MessageObject.success(success);
    }

    @Override
    public MessageObject<Boolean> saveOrUpdateBatch(List<T> entityList) {
        Boolean success = this.getBaseService().saveOrUpdateBatch(entityList);
        return MessageObject.success(success);
    }

    @Override
    public MessageObject<Boolean> removeById(String id) {
        Boolean success = getBaseService().removeById(id);
        return MessageObject.success(success);
    }

    @Override
    public MessageObject<Boolean> removeByMap(Map<String, Object> columnMap) {
        Boolean success = getBaseService().removeByMap(columnMap);
        return MessageObject.success(success);
    }

    @Override
    public MessageObject<Boolean> remove(BaseDataUpdateSqlWrapper<T> queryWrapper) {
        Boolean success = getBaseService().remove(queryWrapper.toUpdateWrapper());
        return MessageObject.success(success);
    }

    @Override
    public MessageObject<Boolean> removeByIds(List<String> idList) {
        Boolean success = getBaseService().removeByIds(idList);
        return MessageObject.success(success);
    }

    @Override
    public MessageObject<Boolean> updateById(T entity) {
        Boolean success = getBaseService().updateById(entity);
        return MessageObject.success(success);
    }

    @Override
    public MessageObject<Boolean> update(EntityUpdateWrapper<T> entityUpdateWrapper) {
        Boolean success = getBaseService().update(entityUpdateWrapper.getEntity(), entityUpdateWrapper.toUpdateWrapper());
        return MessageObject.success(success);
    }

    @Override
    public MessageObject<Boolean> update(BaseDataUpdateSqlWrapper<T> updateWrapper) {
        Boolean success = this.getBaseService().update(updateWrapper.toUpdateWrapper());
        return MessageObject.success(success);
    }

    @Override
    public MessageObject<Boolean> updateBatchById(List<T> entityList, int batchSize) {
        Boolean success = getBaseService().updateBatchById(entityList, batchSize);
        return MessageObject.success(success);
    }

    @Override
    public MessageObject<Boolean> updateBatchById(List<T> entityList) {
        Boolean success = this.getBaseService().updateBatchById(entityList);
        return MessageObject.success(success);
    }

    @Override
    public MessageObject<Boolean> saveOrUpdate(T entity) {
        Boolean success = getBaseService().saveOrUpdate(entity);
        return MessageObject.success(success);
    }

    @Override
    public MessageObject<Boolean> saveOrUpdate(EntityUpdateWrapper<T> entityUpdateWrapper) {
        Boolean success = this.getBaseService().saveOrUpdate(entityUpdateWrapper.getEntity(),
                entityUpdateWrapper.getWrapper().toUpdateWrapper());
        return MessageObject.success(success);
    }

    @Override
    public MessageObject<T> getById(String id) {
        T t = getBaseService().getById(id);
        return MessageObject.success(t);
    }

    @Override
    public MessageObject<List<T>> listByIds(List<String> idList) {
        List<T> list = (List<T>) getBaseService().listByIds(idList);
        return MessageObject.success(list);
    }

    @Override
    public MessageObject<List<T>> listByMap(Map<String, Object> columnMap) {
        List<T> list = (List<T>) getBaseService().listByMap(columnMap);
        return MessageObject.success(list);
    }

    @Override
    public MessageObject<T> getOne(BaseDataQuerySqlWrapper<T> queryWrapper) {
        T t = getBaseService().getOne(queryWrapper.toQueryWrapper(), false);
        return MessageObject.success(t);
    }

    @Override
    public MessageObject<Map<String, Object>> getMap(BaseDataQuerySqlWrapper<T> queryWrapper) {
        Map<String, Object> map = getBaseService().getMap(queryWrapper.toQueryWrapper());
        return MessageObject.success(map);
    }

    @Override
    public MessageObject<Integer> count(BaseDataQuerySqlWrapper<T> queryWrapper) {
        Integer count = getBaseService().count(queryWrapper.toQueryWrapper());
        return MessageObject.success(count);
    }

    @Override
    public MessageObject<Integer> count() {
        Integer count = getBaseService().count();
        throw new InternalApiException("测试错误");
    }

    @Override
    public MessageObject<List<T>> list(BaseDataQuerySqlWrapper<T> queryWrapper) {
        List<T> list = getBaseService().list(queryWrapper.toQueryWrapper());
        return MessageObject.success(list);
    }

    @Override
    public MessageObject<List<T>> list() {
        List<T> list = this.getBaseService().list();
        return MessageObject.success(list);
    }

    @Override
    public MessageObject<Page<T>> page(PageWrapper<T> pageWrapper) {
        Page<T> page = (Page<T>) this.getBaseService().page(pageWrapper.getPage(),
                pageWrapper.getWrapper().toQueryWrapper());
        return MessageObject.success(page);
    }

    @Override
    public MessageObject<Page<T>> page(Page<T> page) {
        Page<T> p = (Page<T>) this.getBaseService().page(page);
        return MessageObject.success(p);
    }

    @Override
    public MessageObject<Page<T>> page(BaseDataQuerySqlWrapper<T> querySqlWrapper) {
        Long[] integers = querySqlWrapper.getOtherBlock().getPage();
        Page<T> page = new Page<>(integers[0], integers[1]);
        page = (Page<T>) this.getBaseService().page(page, querySqlWrapper.toQueryWrapper());
        return MessageObject.success(page);
    }

    @Override
    public MessageObject<Page<Map<String, Object>>> pageMaps(Page<T> page) {
        Page<Map<String, Object>> p = (Page<Map<String, Object>>) this.getBaseService().pageMaps(page);
        return MessageObject.success(p);
    }

    @Override
    public MessageObject<Page<Map<String, Object>>> pageMaps(PageWrapper<T> pageWrapper) {
        Page<Map<String, Object>> p = (Page<Map<String, Object>>) this.getBaseService().pageMaps(pageWrapper.getPage(),
                pageWrapper.getWrapper().toQueryWrapper());
        return MessageObject.success(p);
    }

    @Override
    public MessageObject<List<Map<String, Object>>> listMaps(BaseDataQuerySqlWrapper<T> queryWrapper) {
        List<Map<String, Object>> list = getBaseService().listMaps(queryWrapper.toQueryWrapper());
        return MessageObject.success(list);
    }

    @Override
    public MessageObject<List<Map<String, Object>>> listMaps() {
        List<Map<String, Object>> list = this.getBaseService().listMaps();
        return MessageObject.success(list);
    }
}
